<template>
  <j-modal
    :title="'生成数据集'"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
             <a-col :span="24">
              <a-form-model-item label="槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataSoltIds">
                <!-- <j-select-data-slot
                placeholder="请选择槽号"
                :multi="true"
                :buttons="false"
                v-model="model.dataSoltIds"
              ></j-select-data-slot> -->
              <a-select show-search placeholder="请选择槽号" v-model="model.dataSoltIds">
                <a-select-option v-for="item in slotIdList" :key="item" :value="item">
                  {{ item }}
                </a-select-option>
              </a-select>

              </a-form-model-item>
            </a-col>
             <a-col :span="24">
              <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startEndDate">
                <!-- {{model.beginDate}}  至  {{model.endDate}} -->
                  <a-range-picker v-model="model.startEndDate" />
              </a-form-model-item>
            </a-col> 
            <a-col :span="24">
              <a-form-model-item label="数据源" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <a-select placeholder="请选择" default-value="日报数据集" v-model="model.dsType">
                    <a-select-option value="日报数据集">日报数据集</a-select-option>
                    <a-select-option value="实时数据集">实时数据集</a-select-option> 
                </a-select>
              </a-form-model-item>
            </a-col> 
            <a-col :span="24">
              <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                <a-input v-model="model.name" placeholder="请输入名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="">
                <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { duplicateCheck } from '@/api/api'
import JSelectDataSlot from '@/components/jeecgbiz/JSelectDataSlot2'
import moment from 'moment'
export default {
  name: 'BuildModal',
  components: {
    JSelectDataSlot,
  },
  data() {
    return {
      title: '',
      width: 800,
      visible: false,
      disableSubmit: false,
      slotIdList:[],
      model: {
        name: '',
        remark:'',
        dataSize: '',
        slotNo:'',
        startEndDate:[],
        beginDate:'',
        endDate:'',
        dsType:'日报数据集',
        dataSoltIds: [],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        dataSoltIds:[
           { required: true, message: '请选择槽号!' }
        ],
        startEndDate: [
           { required: true, message: '请选择日期!' }
        ],
        name: [
           { required: true, message: '请输入名称!' },
           { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
           { validator: this.validateName }
        ],
      },
      url: {
        add: '/dataSet/dataSet/add',
      },
    }
  },
  created() { 
    this.loadDataSlot()
  },
  methods: {
    add() {
      Object.assign(this.model, {
         name: '',
        remark:'',
        dataSize: '',
        slotNo:'',
        startEndDate:[],
        beginDate:'',
        endDate:'',
        dsType:'日报数据集',
        dataSoltIds: [],
      }) 
      this.visible = true 
    }, 
    close() {
      this.$emit('close')
      this.visible = false
    },
    loadDataSlot() {
      getAction('/dataSlot/dataSlot/list', { pageSize: 9999 })
        .then((res) => {
          if (res.success) {
            var dataSource = res.result.records
            for (let i = 0; i < dataSource.length; i++) {
              this.slotIdList.push(dataSource[i].slotId)
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleOk() { 
      const that = this  
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (that.model.startEndDate) {
            that.model.beginDate = moment(that.model.startEndDate[0]).format('YYYY-MM-DD')
            that.model.endDate = moment(that.model.startEndDate[1]).format('YYYY-MM-DD')
          } else {
            this.$message.warning('请选择日期')
            return
          } 
          that.model.slotNo = that.model.dataSoltIds.trim();
          that.confirmLoading = true
          httpAction(this.url.add, this.model, 'post')
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.visible = false
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateName(rule, value, callback) {
        var params = {
          tableName: 'tb_data_set',
          fieldName: 'name',
          fieldVal: value,
          dataId: '',
        }
      duplicateCheck(params).then((res) => {
        if (res.success) {
          callback()
        } else {
          callback('名称已存在!')
        }
      })
    },
  },
}
</script>